from sqlalchemy import Column, Integer, String, DateTime, Text, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..base import Base


class ProductCategory(Base):
    """
    商品分类模型
    """
    __tablename__ = "product_categories"

    id = Column(Integer, primary_key=True, index=True, comment="分类ID")
    name = Column(String(100), nullable=False, comment="分类名称")
    description = Column(Text, nullable=True, comment="分类描述")
    parent_id = Column(Integer, ForeignKey("product_categories.id", ondelete="CASCADE"), nullable=True, comment="父分类ID，支持层级分类")
    sort_order = Column(Integer, default=0, comment="排序顺序")
    is_active = Column(Integer, default=1, comment="是否激活：1-激活，0-禁用")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")

    # 自引用关系
    parent = relationship("ProductCategory", remote_side=[id], backref="children")

    def __repr__(self):
        return f"<ProductCategory(id={self.id}, name='{self.name}')>"